<?php
/**
 * Outputs the HTML for the Options page
 */
function webtreats_admin_options() {
global $themename, $shortname, $options;
?>

<?php screen_icon('options-general'); ?>
<h2 class="webtreats-page-title"><?php echo $themename; ?> Theme Settings</h2>

<?php
if ( $_REQUEST['saved'] ) $print_msg = '<div id="message" class="updated fade below-h2"><p><strong>'.$themename.' settings saved.</strong></p></div>';
if ( $_REQUEST['error'] ) $print_msg = '<div id="message" class="updated fade below-h2"><p><strong>Sidebar already exists, please use a different name.</strong></p></div>';
if ( $_GET['upgraded'] )  echo '<div id="message" class="updated fade below-h2"><p><strong>'.$themename.' is now activated.</strong></p></div>';
?>

<div class="wrap" id="webtreats_container">
	<div id="main">
        <div id="webtreats_nav">
			<ul id="tabs" class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
				<li class="ui-state-default ui-corner-top"><a title="General Settings" href="#generalsettings">General Settings</a></li>
				<li class="ui-state-default ui-corner-top"><a title="Homepage Test" href="#homepage">Homepage</a></li>
				<li class="ui-state-default ui-corner-top"><a title="General Settings" href="#sociable">Sociables</a></li>
				<li class="ui-state-default ui-corner-top"><a title="Homepage Settings" href="#slidersetting">Slider</a></li>
				<li class="ui-state-default ui-corner-top"><a title="Blog Settings" href="#blog">Blog</a></li>
				<li class="ui-state-default ui-corner-top"><a title="Sidebar Settings" href="#sidebar">Sidebar</a></li>
				<li class="ui-state-default ui-corner-top"><a title="Footer Settings" href="#footersettings">Footer</a></li>
				<li class="ui-state-default ui-corner-top"><a title="Navagation Settings" href="#navsettings">Navagation</a></li>
			</ul>
		</div><!--webtreats_nav-->
	<div id="admin_content">
			
<form method="post" action="">
	
<input id="hidden_anchor" type="hidden" name="hidden_anchor" value="" />
				
<?php
/* Get Categories into a drop-down list */
$webtreats_categories_obj = get_categories('hide_empty=0');
$webtreats_categories = array();
$webtreats_categories[] = '';
foreach ($webtreats_categories_obj as $webtreats_cat) {
	$webtreats_categories[$webtreats_cat->cat_ID] = $webtreats_cat->cat_name;
}

/* Get Pages into a drop-down list */
$pages_list = get_pages();
$getpagnav = array();
foreach($pages_list as $apage) {
	$getpagnav[$apage->ID] = $apage->post_title;
}

$get_options = get_option($shortname.'_general_settings');

foreach ($options as $value) {
$id = $value['id'];
$selector = ($value['selector']) ? ' class="' .$value['selector']. '"' : '';

switch ( $value['type'] ) {

case 'opentab':
?>
<div id="<?php echo $value['id']; ?>" class="group">
	<h2><?php echo $value['name']; ?></h2>
	<?php echo $print_msg; ?>
	<?php if($value['wp3_0'] == true) {echo $value['msg']; }?>
<?php

break;
case 'closetab':
?>
</div>
<?php

break;
case 'table_open':
?>
<table class="form-table webtreats-options">
<?php

break;
case 'title_h3':
?>
<h3><?php echo $value['name']; ?></h3>
<table class="form-table webtreats-options">
<?php

break;
case 'table_close':
?>
</table>
<?php

break;
case 'text':
$selected_option = $value['selected_option'];
$selected_current = '';
if($selected_option){
	$selected_current = ($get_options[$selected_option] != $value['selected']) ? 'style="display:none;"' : '';
}
?>
<tr <?php echo $value['selected_id']; ?> <?php echo $value['selected_class']; ?> <?php echo $selected_current; ?> valign="top">
<th align="left"><?php echo $value['name']; ?></th>
<td><input size="<?php echo $value['size']; ?>" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php echo stripslashes(htmlspecialchars($get_options[$id])); ?>" />
<br /><span><?php echo $value['desc']; ?></span></td>
</tr>
<?php 

break;
case 'textarea':
$selected_option = $value['selected_option'];
$selected_current = '';
if($selected_option){
	$selected_current = ($get_options[$selected_option] != $value['selected']) ? 'style="display:none;"' : '';
}
?>
<tr <?php echo $value['selected_id']; ?> <?php echo $value['selected_class']; ?> <?php echo $selected_current; ?> valign="top">
<th><?php echo $value['name']; ?></th>
<td><textarea name="<?php echo $value['id']; ?>" cols="62" rows="7"><?php if ( $get_options[$id] != "") { echo stripslashes(htmlspecialchars($get_options[$id])); } else { echo $value['std']; } ?></textarea>
<br /><span><?php echo $value['desc']; ?></span></td>
</tr>
<?php

break;
case 'select':
$selected_option = $value['selected_option'];
$selected_current = '';
if($selected_option){
	$selected_current = ($get_options[$selected_option] != $value['selected']) ? 'style="display:none;"' : '';
}
?>
<tr <?php echo $value['selected_id']; ?> <?php echo $value['selected_class']; ?> <?php echo $selected_current; ?>>
<th align="left"><?php echo $value['name']; ?></th>
<td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
	
<?php 
if($value['options'] == 'pages_list') : ?><option value="">Select Page</option><?php 
endif;

$get_options[$id] = ($get_options[$id]) ? $get_options[$id] : $value['std'];
	
if($value['options'] == 'pages_list'){

foreach ($getpagnav as $pgid => $pgnaame){

if ($get_options[$id] == $pgid){
$selected = "selected='selected'";
	}else{
$selected = "";		
}
echo"<option $selected value='". $pgid."'>". $pgnaame."</option>";
}
	
}else{
	
foreach ($value['options'] as $option){

if ($get_options[$id] == $option){
$selected = "selected='selected'";
	}else{
$selected = "";
}
echo"<option $selected value='". $option."'>". $option."</option>";
}

}
?>
</select>
<br /><span><?php echo $value['desc']; ?></span></td>
</tr>
<?php

break;
case 'radio':
?>
<tr>
<th valign="top"><?php echo $value['name']; ?></th>
<td>
	
<?php
$get_options[$id] = ($get_options[$id]) ? $get_options[$id] : $value['std'];
foreach ($value['desc'] as $desc => $value) { ?>

<label><input name="<?php echo $id; ?>" id="<?php echo $id; ?>" type="radio" value="<?php echo $value; ?>" <?php echo $selector; ?> <?php if ($get_options[$id] == $value){echo 'checked="checked"';}?> /> <?php echo $desc; ?></label><br /> 

<?php } ?>	

</td>
</tr>
<?php

break;
case 'checkbox':
?>
<tr>
<th align="left"><?php echo $value['name']; ?></th>
<td width="80%">
<?php $checked = ( $get_options[$id] ) ? "checked=\"checked\"" : ''; ?>
<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />&nbsp;&nbsp;<span><?php echo $value['desc']; ?></span>
</td>
</tr>
<?php

break;
case 'exclude_include_checkbox':
?>
<tr valign="top">
<th align="left"><?php echo $value['name']; ?></th>
<td id="<?php echo $value['id']; ?>" class="webtreats_box_select">
<?php
if($get_options[$id]){ $checked = "checked=\"checked\""; }else{ $checked = "";} 
$value['options'] = ( $value['options'] == 'webtreats_categories' ) ? $webtreats_categories : $getpagnav;
foreach ($value['options'] as $webtreats_pgid => $webtreats_pgname) {
if ($webtreats_pgname !='') {
echo '<div style="border:1px solid #cccccc;margin-right:4px;margin-bottom:4px;padding:4px;white-space:nowrap;float:left;width:120px;overflow:hidden;"><input type="checkbox" name="'.$webtreats_pgid.'" id="'.$value['id'].'_'.$webtreats_pgid.'" class="select_' .$value['id']. '" /> '.$webtreats_pgname.'</div>';
}
}
?>

<div style="clear:both"></div>
<input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="text" value="<?php if ( $get_options[$id] != '') { echo stripslashes($get_options[$id]); } else { echo $value['std']; } ?>" />
<br /><span><?php echo $value['desc']; ?></span></td>
</tr>
<?php

break;
case 'slider_source':
?>
<?php $slider_source = ( ($get_options['homepage_slider_source'] == $value['class']) || ($value['std'] && $get_options['homepage_slider_source'] == "") ) ? '' : ' style="display:none"'; ?>
<div id="toggle_click_source" class="selected_source_<?php echo $value['class']; ?>"<?php echo $slider_source; ?>>
<?php

break;
case 'slider_close':
?>
</div>
<?php

break;
case 'slider':
?>
<?php
if(!$get_options['homepage_slider']){$get_options['homepage_slider'] = $value['std'];}
$slider_current = ($get_options['homepage_slider'] == $value['selected']) ? true : false;
$slider_options = get_option($shortname.'_slider_settings');
$slider_count = ($slider_options[$value['selected'].'_slider_count'])<=0 ? 1 : $slider_options[$value['selected'].'_slider_count'];
?>
<div id="toggle_click" class="<?php if($slider_current){echo 'multitables ';} ?>selected_<?php echo $value['selected']; ?>"<?php if(!$slider_current){echo ' style="display:none"';} ?>>

<input name="<?php echo $value['selected']; ?>_slider_count" class="<?php if($slider_current){echo 'slider_counter ';} ?>count_hide_rm" type="hidden" value="<?php echo $slider_count; ?>" />

<p><input type="submit" class="button-secondary apply add_row" value="Click to add new row" /></p>

<table class="widefat table_sort">
	
<thead>
<tr>
<th width="60px">Reorder</th>
<th>Delete</th>
<th width="460px">Setting</th>
<th>Description</th>
</tr>
</thead>

<tfoot>
<tr>
<th>&nbsp;</th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>

<tbody>
<?php 
$count = $slider_count +1;
for($z = 0; $z < $count; $z++) {
?>
<tr class="<?php if($slider_current){echo 'multitable ';} ?>multitable_hide_rm<?php if ($z+1 == $count) echo ' hidden'; if($z+1 == $count && $slider_current) echo ' clone_row';?>">
	
<td class="slider_drag">
<span class="changenumber"><?php echo $z+1; ?></span>
</td>

<td>
<a href='#' title="Delete Row" class='del_row' id='del_number_<?php echo $z + 1; ?>'><img src="<?php echo get_template_directory_uri() .'/lib/admin/images/slider_delete.png'; ?>"  alt="Delete Row" /></a>
</td>

<td>
<div style="float:left;width:76px;">Image/Video</div>
<input class="correct_num" id="<?php echo $value['id']; ?>_url_<?php echo $z; ?>" name="<?php echo $value['id']; ?>_url_<?php echo $z; ?>" type="text" value="<?php echo $slider_options[$value['selected'].'_slider_url_'.$z] ?>" size="25"/><br />
<div style="float:left;width:76px;">Link URL</div>
<input class="correct_num" id="<?php echo $value['id']; ?>_link_<?php echo $z; ?>" name="<?php echo $value['id']; ?>_link_<?php echo $z; ?>" type="text" value="<?php echo stripslashes(htmlspecialchars($slider_options[$value['selected'].'_slider_link_'.$z])); ?>" size="25"/><br />
<div style="float:left;width:76px;">Title</div>
<input class="correct_num" id="<?php echo $value['id']; ?>_title_<?php echo $z; ?>" name="<?php echo $value['id']; ?>_title_<?php echo $z; ?>" type="text" value="<?php echo stripslashes(htmlspecialchars($slider_options[$value['selected'].'_slider_title_'.$z])); ?>" size="25"/><br />
<div style="float:left;width:76px;">Stage Effect</div>
<select class="correct_num" id="<?php echo $value['id']; ?>_stage_<?php echo $z; ?>" name="<?php echo $value['id']; ?>_stage_<?php echo $z; ?>">

	
<?php
$stage_effect = array( "Staged" => "staged_slide", 
	   "Partial Staged" => "partial_staged_slide",
	    "Partial Gradient" => "partial_gradient_slide",
	    "Floating" => "floating_slide",
	    "Full" => "full_slide");
foreach ($stage_effect as $stage_effect => $stage_value) {
	
if ($slider_options[$value['selected'].'_slider_stage_'.$z] == $stage_value && $z+1 != $count){
$selected = "selected='selected'";	
}else{
$selected = "";		
}
echo"<option $selected value='". $stage_value."'>". $stage_effect."</option>";
}
?>


</select><br />
<?php if($slider_options[$value['selected'].'_slider_btn_'.$z]){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
<div style="float:left;width:150px;padding-right:1px">Disable "Read More" Button</div>
<input class="correct_num" type="checkbox" id="<?php echo $value['id']; ?>_btn_<?php echo $z; ?>" name="<?php echo $value['id']; ?>_btn_<?php echo $z; ?>" value="true" <?php echo $checked; ?> />
</td>

<td>
<textarea rows="5" cols="35" class="correct_num" id="<?php echo $value['id']; ?>_desc_<?php echo $z; ?>" name="<?php echo $value['id']; ?>_desc_<?php echo $z; ?>"><?php echo stripslashes(htmlspecialchars($slider_options[$value['selected'].'_slider_desc_'.$z])); ?></textarea>
</td>

</tr>
<?php } ?>
</tbody>
	
</table>
	
</div>
<?php

break;
case 'sidebar':
?>
<tr valign="top">
<th align="left"><?php echo $value['name']; ?></th>
<td><input size="<?php echo $value['size']; ?>" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="text" value="<?php if ( $get_options[$id] != "") { echo $get_options[$id]; } else { echo $value['std']; } ?>" />
<br /><span><?php echo $value['desc']; ?></span></td>
</tr>
<?php 

break;
case 'sidebar_delete':
?>
<?php
$get_sidebar_options = sidebar_generator_webtreats::get_sidebars();
if($get_sidebar_options != "") {
$i=1;

foreach ($get_sidebar_options as $sidebar_gen) { ?>
<?php if($i == 1) { ?>
<tr valign="top">
<th align="left"></th>
<td><div align="left"><h3 style="margin-bottom:0px"><?php echo $value['desc']; ?></h3></div></td>
</tr>
<?php } ?>

<tr id="sidebar_table_<?php echo $i; ?>">
<th align="left"></th>
<td>
<div align="left" style="float:left;font-size:13px;width:150px;overflow:hidden;"><?php echo $sidebar_gen; ?></div>
<div style="float:left;"><input type="submit" name="sidebar_rm_<?php echo $i; ?>" id="<?php echo $i; ?>" class="button" value="Delete" /></div>
<div style="margin:3px 0 0 8px;float:left;"><img class="sidebar_rm_<?php echo $i; ?>" style="display:none;" src="images/wpspin_light.gif" alt="Loading" /></div>
</td>
<th align="left"><input id="<?php echo 'sidebar_generator_'.$i ?>" type="hidden" name="<?php echo 'sidebar_generator_'.$i ?>" value="<?php echo $sidebar_gen; ?>" /></th>
</tr>
<?php $i++;  
} 
}

break;
case 'submit':
?>
<div class="submit webtreats-submit"><input class="button-primary" type="submit" name="save" value="Save changes"/><input type="hidden" name="action" value="save" /></div>
<?php

break;
case 'frontpage_content_editor':
?>
<h2 style="margin:20px 0 35px 0;"><?php echo $value['name']; ?></h2>
<div id="poststuff"><div id="post-body"><div id="post-body-content"><div class="postarea" id="postdivrich">
	
<?php
$the_editor_content = stripslashes($get_options['content']);
the_editor($the_editor_content); 
?>

<table id="post-status-info" cellspacing="0">
	<tbody><tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	</tr></tbody>
</table>
</div></div></div></div>

<?php 

break;

	
} //close switch
} //close foreach
?>	
		
</form>
</div><!--content-->
<div class="clear"></div>
</div><!--main-->
<div class="clear"></div>
</div><!--wrap-->

<?php }

/**
 * Start Admin Functions
 */
function webtreats_add_admin_options() {
	global $themename, $shortname, $options ,$page_handle;
	$hidden_anchor = $_REQUEST['hidden_anchor'];
	$send = $_GET['page'];
	if ( $_GET['page'] == $page_handle ) {
	if ( 'save' == $_REQUEST['action'] ) {
		
		// Updates homepage slider settings
		foreach ($_POST as $key => $value) {
			if ( preg_match("/(fading_slider_|tabbed_slider_|cycling_slider_|accordion_slider_|coin_slider_)/i", $key) ) {
				$options_slider[$key] = $value;	
			}
		}
		update_option( $shortname.'_slider_settings', $options_slider);
		
		//Updates genearal settings
		foreach ($options as $value) {		
			if($value['id'] != 'sidebar_generator_0'){
				$options_array[$value['id']] = $_REQUEST[ $value['id'] ]; 
			}
		}
		update_option( $shortname.'_general_settings', $options_array);
			
		//Updates sidebar settings
		$get_sidebar_options = sidebar_generator_webtreats::get_sidebars();
		$sidebar_name = str_replace(array("\n","\r","\t"),'',$_POST['sidebar_generator_0']);
		$sidebar_id = sidebar_generator_webtreats::name_to_class($sidebar_name);
		if($sidebar_id == '' ){
			$options_sidebar = $get_sidebar_options;
		}else{
			if(isset($get_sidebar_options[$sidebar_id])){
				header("Location: admin.php?page=$send&error=true$hidden_anchor");	
				die;
			}
			if ( is_array($get_sidebar_options) ) {
				$new_sidebar_gen[$sidebar_id] = $sidebar_id;
				$options_sidebar = array_merge($get_sidebar_options, (array) $new_sidebar_gen);	
			}else{
				$options_sidebar[$sidebar_id] = $sidebar_id;
			}		
		 }

		update_option( $shortname.'_sidebar_generator', $options_sidebar);
		header("Location: admin.php?page=$send&saved=true$hidden_anchor");
		die;
			} else if( 'reset' == $_REQUEST['action'] ) {
		foreach ($options as $value) {
			delete_option( $value['id'] ); }
			header("Location: admin.php?page=$send&reset=true$hidden_anchor");
		die;
		}
	}
		add_menu_page($themename." Options", $themename, 8, $page_handle, 'webtreats_admin_options');
}

function ajax_update_widgets($sidebar_id) {
	$get_widgets = wp_get_sidebars_widgets();
	unset( $get_widgets['array_version'] );

	$before_delete = true; $i=0;
	foreach ($get_widgets as $key => $value) {
		if( !preg_match('/webtreats_sidebar-([0-9]+)/', $key) ) {
				$update_widgets[$key] = $value;			
		}
		if( preg_match('/webtreats_sidebar-([0-9]+)/', $key) ) {
			if($key == "webtreats_sidebar-$sidebar_id") {
				$before_delete = false; $inactive_widgets = $value; }
			if( ($key != "webtreats_sidebar-$sidebar_id") && ($before_delete == true) ) {
				$update_widgets[$key] = $value; }		
			if( ($key != "webtreats_sidebar-$sidebar_id") && ($before_delete == false) ) {
				$update_widgets['webtreats_sidebar-'.$i] = $value; }
			$i++;
		}
	}
	$update_widgets['wp_inactive_widgets'] = array_merge($inactive_widgets, (array) $update_widgets['wp_inactive_widgets']);
	wp_set_sidebars_widgets($update_widgets);
}

function ajax_sidebar_rm() {	
	global $shortname, $wpdb;
	$sidebar = $_POST['sidebar'];
	$sidebar_id = $_POST['sidebar_id'];
	$sidebar_name = $_POST['sidebar_name'];
	$pieces = explode(",", $sidebar);

	foreach ($pieces as $key => $value) {
		if($value != '')
			$options_sidebar_rm[ $value ] = $value;
		}
		update_option( $shortname.'_sidebar_generator', $options_sidebar_rm);
		ajax_update_widgets($sidebar_id);

		$sidebar_meta = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '$sidebar_name'", ARRAY_A);
		if ( is_array($sidebar_meta) ){
			foreach ($sidebar_meta as $key => $value) {
				delete_post_meta($value['post_id'], 'selected_sidebar');
		}
	}
}

add_action('wp_ajax_sidebar_rm', 'ajax_sidebar_rm');
add_action('wp_ajax_show_hide_pgs', 'ajax_show_hide_pgs');
add_action('admin_menu', 'webtreats_add_admin_options');

?>